package com.gadgets.framework.common.advisor;

import com.gadgets.framework.common.Fm04Constant;
import com.gadgets.framework.common.anno.WoodenCheck;
import com.gadgets.framework.common.constant.GadgetsConstant;
import com.gadgets.framework.common.util.CacheUtils;
import com.gadgets.framework.common.util.RedisUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

/**
 * 功能说明：
 *
 * @author Unclexiu
 * @version V202101.00.000
 * @data 2022-07-19
 * 修改记录：  修改日期          修改人员    修改说明
 * 2022-07-19  xiujunqing   创建文件
 */
@Component
public class WoodenAdvisor extends HandlerInterceptorAdapter {

	@Autowired
	private RedisUtil redisUtil;
	@Autowired
	private CacheUtils cacheUtils;

	private static final Logger logger = LoggerFactory.getLogger(HandlerInterceptorAdapter.class);

	/**
	 * This implementation always returns {@code true}.
	 *
	 * @param request
	 * @param response
	 * @param handler
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		if (GadgetsConstant.NUMBER_CONSTANT.STR_NUMBER_1.equals(cacheUtils.getSysParamCache(Fm04Constant.WOODEN_CHECK_PARAM))) {
			logger.info("令牌校验已开启，开始尝试获取令牌");
			if (null != request && handler instanceof HandlerMethod) {
				Method targetMethod = ((HandlerMethod) handler).getMethod();
				Class<?> targetClass = ((HandlerMethod) handler).getMethod().getDeclaringClass();
				WoodenCheck check = ((HandlerMethod) handler).getMethodAnnotation(WoodenCheck.class);
				if(null!=check){
					// 如果存在 @WoodenCheck 注解
					String[] woodenKey = check.woodenKey();
					for (String s : woodenKey) {

					}
				}
			}
		}
		return true;
	}

}
